<?php
/**
*
* xt=login 要login_type
* xt=bind 要bind_type
* xt=share 要share_to
* xt=search 要search_type,keyword,is_include
*/
class report {
	var $param = array();
	var $allow = array('login','bind','share','search'); 
	var $req_method = array();
	var $uid = 0;
	var $cookie_name_prefix = 'report_';
	var $cid = '';
	
	function prepare(){
		$c_value = md5(microtime());
		$c_key   = $this->cookie_name_prefix . 'cid';
		$a=$_COOKIE;
		if (!isset($_COOKIE[$c_key])||empty($_COOKIE[$c_key])) {
			$this->cid = $c_value;	
			setcookie($c_key, $c_value, time()+3600*24*30);
			$_COOKIE[$c_key]=$c_value;
		} 
		if (isset($_COOKIE[$c_key])) {
			$this->cid = $_COOKIE[$c_key];	
		}
	}

	/**
	 * 初始化数据
	 */
	function init( $flag='' ) {
		$this->param = array(
			'ver'=>WB_VERSION,
			'ip'=>F::ip(),
			'source' => 'pc'
		);
		$this->req_method = array(
				'img' => array('login','bind','share','search'), // 前端输出<img>延迟上报
				'http' => array(), // 后端同步上报
				);
		$this->uid = USER::uid()? USER::uid() : 0;
		$this->param['cid'] = $this->cid;
	}

	/**
	 * 取得延时数据，并返回对应的<img>标签,
	 * @return string
	 */
	function flush() 
	{
		if ( empty($this->cid) ) { return; }
		$params = MC::get('report', $this->cid);
		MC::del('report', $this->cid);
		if ($params && is_array($params)) {
			$block_size = 20;
			$count = count($params);
			$content = '';
			for ($i=0,$n=ceil($count / $block_size); $i<$n; $i++) {
				$block = array_slice($params,$i, ($i+1)*$count);
				$content .= '<img style="display:none" src="'.'http://beacon.ziyou.com/a.gif?data='.urlencode(json_encode(array_values($block))) . '&r='.rand(). '" />'; 
			}
			return $content;
			//return '<img style="display:none" src="'.'http://beacon.ziyou.com/a.gif?data='.urlencode(json_encode(array_values($params))) . '&r='.rand(). '" />'; 
		}
		return '';
	}
	

	/**
	 * 返回cid
	 *
	 */
	function getCid() {
		return $this->cid;
	}


	/**
	 * 上报
	 * @param $flag string|array 为string：则上报标识$flag指定的数据,如果为数据则表示为指上报数据
	 * @param $data array 要上报的数据
	 * @return boolean|string
	 */
	function push($flag=null, $data=array()) {
		
		$this->init($flag);

		// 如果$flag为null，则尝试输出延迟上报
		if ($flag === null) {
			return;
		}
		if (!is_array($flag)) {
			if (!is_string($flag)) {
				return false;
			}
			$flag = array($flag=>$data);
		}


		
		$group_req = array(); // 格式化后的数据,格式如array('http'=>array('publish'=>array({具体数据1}),'delete'=>array({具体数据2})..))
		foreach ($flag as $k=>$d) {
			if (!in_array($k, $this->allow)) {
				return false;
			}
			foreach ($this->req_method as $key=>$value) {
				if (in_array($k, $value)) {
					$group_req[$key][$k] = array_merge($d, $this->default_param($k));
				}
			}
		}
		if (!empty($group_req)) {
			foreach ($group_req as $method=>$req) {
				switch ($method) {
					case 'img':
						$prev = MC::get('report', $this->cid);
						if ($prev && is_array($prev)) {
							$req = array_merge($prev, array_values($req));
						}
						MC::set('report', $this->cid, array_values($req));
						break;
					case 'http':
						//@todo 即时上报
						break;
				}
			}
		}
		return true;
	}

	/**
	 * 取得默认参数
	 * @param $flag string
	 * @return array
	 */
	function default_param($flag) {
		$params = array(
			'uid' => $this->uid,
			'xt' => $flag
		);
		$params['uid'] = $this->uid;
		switch ($flag) {
			case 'login':
			break;
			case 'bind':
			break;
			case 'share':
			break;
			case 'search':
			break;
			case 'stay':
				$params['time'] = time();
			break;
		}
		return array_merge($params, $this->param);
	}
}

?>
